package basics;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Dataset<F, C> {

	private ArrayList<F> _features = new ArrayList<F>();
	private ArrayList<C> _classVal = new ArrayList<C>();

	public DatasetItem<F, C> getItem(int index) {
		return new DatasetItem<F, C>(_features.get(index), _classVal.get(index));
	}

	public List<F> features() {
		return _features;
	}

	public List<C> classes() {
		return _classVal;
	}

	public int size() {
		return _features.size();
	}

	public F feature(int i) {
		return _features.get(i);
	}

	public C classVal(int i) {
		return _classVal.get(i);
	}

	public void add(DatasetItem<F, C> item) {
		_features.add(item.getKey());
		_classVal.add(item.getValue());
	}

	public Dataset<F, C> extractRandomSubset(double percent) {
		int n = this.size();
		Dataset<F, C> ds = new Dataset<F, C>();
		Random r = new Random();

		for (int i = 0; i < (n * percent); i++) {
			int idx = r.nextInt(this.size());
			ds.add(this.getItem(idx));
			this.remove(idx);
		}

		return ds;
	}

	private void remove(int idx) {
		_features.remove(idx);
		_classVal.remove(idx);
	}

}
